<template>
  <div class="table-component">
    
    <!-- 上方工具栏 -->
    <div v-if="datas.showToolbar && datas.toolbarPosition === 'top'" class="table-toolbar">
      <div class="table-toolbar-title">
        <span>{{ datas.text || '表格数据' }}</span>
      </div>
      <div class="table-toolbar-buttons">
        <el-button v-if="datas.showRefresh" size="small" icon="Refresh" circle title="刷新"></el-button>
        <el-button v-if="datas.showFullscreen" size="small" icon="FullScreen" circle title="全屏"></el-button>
        <el-button v-if="datas.showColumnSelect" size="small" icon="Setting" circle title="列设置"></el-button>
      </div>
    </div>
    <el-row>
      <!-- 表格 -->
      <el-table
        :data="datas.data"
        :border="datas.border"
        :stripe="datas.stripe"
        :show-header="datas.showHeader"
        :highlight-current-row="datas.highlightCurrentRow"
        :height="datas.height && datas.height !== '100%' ? datas.height : null"
        :max-height="datas.maxHeight || null"
        class="custom-table"
        style="width: 100%"
      >
        <!-- 索引列 -->
        <el-table-column 
          v-if="datas.showIndex"
          type="index"
          width="50"
        />
        
        <!-- 动态列 -->
        <el-table-column
          v-for="(column, index) in datas.columns"
          :key="index"
          :prop="column.prop"
          :label="column.label"
          :width="column.width || ''"
          :sortable="column.sortable"
          :align="column.align"
        />
        
        <!-- 操作列 -->
        <el-table-column
          v-if="datas.showOperationColumn"
          :label="datas.operationColumnLabel || '操作'"
          :width="datas.operationColumnWidth || '150'"
          align="center"
          fixed="right"
        >
          <template #default>
              <el-row :gutter="10">
                  <el-col :span="8" v-for="(btn, index) in datas.operationButtons">
                    <el-button
                        :key="index"
                        :type="getButtonType(index)"
                        size="small"
                        link
                    >{{ btn }}</el-button>
                  </el-col>
              </el-row>
          </template>
        </el-table-column>
      </el-table>
    </el-row>

    
    <!-- 下方工具栏 -->
    <div v-if="datas.showToolbar && datas.toolbarPosition === 'bottom'" class="table-toolbar">
      <div class="table-toolbar-info">
        <span>共 {{ datas.data ? datas.data.length : 0 }} 条</span>
      </div>
      <div class="table-toolbar-buttons">
        <el-button v-if="datas.showRefresh" size="small" icon="Refresh" circle title="刷新"></el-button>
        <el-button v-if="datas.showFullscreen" size="small" icon="FullScreen" circle title="全屏"></el-button>
        <el-button v-if="datas.showColumnSelect" size="small" icon="Setting" circle title="列设置"></el-button>
      </div>
    </div>
    
    <!-- 分页 -->
    <div v-if="datas.showPagination" class="pagination-container">
      <el-pagination
        :current-page="1"
        :page-size="datas.pageSize || 10"
        :page-sizes="datas.pageSizes || [10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="datas.data ? datas.data.length : 0"
      />
    </div>
    
  </div>
</template>

<script>
export default {
  name: 'table',
  props: {
    datas: {
      type: Object,
      required: true
    }
  },
  mounted() {
    console.log('[TableComponent] 已挂载，数据:', this.datas);
    
    // 更详细的日志，帮助调试
    console.log('[TableComponent] 工具栏显示:', this.datas.showToolbar);
    console.log('[TableComponent] 操作列显示:', this.datas.showOperationColumn);
    console.log('[TableComponent] 分页显示:', this.datas.showPagination);
    console.log('[TableComponent] 列配置:', this.datas.columns);
    console.log('[TableComponent] 数据行数:', this.datas.data ? this.datas.data.length : 0);
  },
  methods: {
    getButtonType(index) {
      const types = ['primary', 'success', 'danger']; 
      return index < types.length ? types[index] : 'default';
    }
  }
}
</script>

<style scoped lang="less">
.table-component {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;

  
  :deep(.custom-table) {
    flex: 1;
    min-height: 150px; // 设置最小高度，确保表格有合理的展示区域
  }
  
  .table-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 16px;
    background-color: #f5f7fa;
    border: 1px solid #ebeef5;
    border-radius: 4px;
    
    .table-toolbar-title {
      font-size: 14px;
      font-weight: 500;
      color: #606266;
    }
    
    .table-toolbar-info {
      font-size: 13px;
      color: #909399;
    }
    
    .table-toolbar-buttons {
      display: flex;
      gap: 5px;
    }
  }
  
  .pagination-container {
    display: flex;
    justify-content: flex-end;
    padding: 10px 0;
    background-color: #fff;
  }
}
</style> 